package cn.lzit.request.servlet;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "CircleAreaServlet", urlPatterns = "/CircleAreaServlet")

public class CircleAreaServlet extends HttpServlet {
    private static final long serialVersionUID=1L;
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.println("<html><body bgcolor='pink'>");
        String strRadius = request.getParameter("r");
        double r = 0,area = 0,circumference = 0;
        try{
            r = Double.parseDouble(strRadius);//数据类型转换
            //计算
            area = 3.14*r*r;
            circumference = 2*3.14*r;
            out.print("<BR>半径:"+r);
            out.print("<BR>圆面积:"+area);
            out.print("<BR>圆周长:"+circumference);
            out.print("</body></html>");
        }catch (Exception e){
            request.setAttribute("error",strRadius+":半径数据不合法");
            RequestDispatcher rd = request.getRequestDispatcher("/error");
            rd.forward(request,response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
